﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuTouTiaoAssist.Util;

namespace QuTouTiaoAssist
{
    public partial class FrmLoginByMobile : FrmBase
    {
        public string MemberToken { get; private set; }

        public FrmLoginByMobile()
        {
            InitializeComponent();
        }

        private void FrmLoginByMobile_Load(object sender, EventArgs e)
        {
            base.EnableCancelButton();
            var deviceCode = QttHelper.RefreshDeviceCode();
            this.txtDeviceCode.Text = deviceCode;
            this.textBox2.Text = ConfigUtil.GetDefaultPassword();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.textBox2.PasswordChar = this.checkBox1.Checked ? '\0' : '*';
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var loginResult = QttHelper.Login(this.textBox1.Text.Trim(), this.textBox2.Text);
                if (loginResult.Code == 0)
                {
                    this.MemberToken = loginResult.Data.Token;
                    var getMemberResult = QttHelper.GetMemberInfo(this.MemberToken);
                    if (getMemberResult.Code == 0)
                    {
                        Log.Write(string.Format("【{0}】登陆成功，手机号为：{1}，设备号为：{2}，Id为：{3}",
                            getMemberResult.Data.NickName,
                            getMemberResult.Data.MobileNumber,
                            QttHelper.GetDeviceCode(),
                            getMemberResult.Data.MemberId));
                    }
                }
                else
                {
                    throw new Exception(loginResult.Message);
                }
            }
            catch (Exception ex)
            {
                MsgBox.ShowInfo("登陆失败，" + ex.Message);
                return;
            }

            this.DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.CancelButton.PerformClick();
        }

        private void btnRandomDeviceCode_Click(object sender, EventArgs e)
        {
            var deviceCode = QttHelper.GenerateDeviceCode();
            FrmDeviceCode.SaveDeviceCode(deviceCode);
            QttHelper.SetDeviceCode(deviceCode);
            this.txtDeviceCode.Text = deviceCode;
        }

        /// <summary>
        /// 选择新注册的用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelectNewRegister_Click(object sender, EventArgs e)
        {
            var cms = new ContextMenuStrip();
            if (GlobalContext.NewRegisterMobilePwdCache.Count < 1)
            {
                cms.Items.Add("无", null, null);
            }
            else
            {
                foreach (KeyValuePair<string, string> item in GlobalContext.NewRegisterMobilePwdCache)
                {
                    cms.Items.Add(item.Key + "," + item.Value, null, (o, args) =>
                    {
                        var mp = (KeyValuePair<string, string>)((o as ToolStripItem).Tag);
                        this.textBox1.Text = mp.Key;
                        this.textBox2.Text = mp.Value;
                    }).Tag = item;
                } 
            }
            var point = this.btnSelectNewRegister.Location;
            point.Y += this.btnSelectNewRegister.Height;
            cms.Show(this, point);
        }
    }
}
